cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-06 LANGUAGES CXX)

# 声明一个 C++11 项目
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Eigen3 3.4 REQUIRED CONFIG)            # 使用Eigen库，在系统上找到它的头文件

# 检查编译器标志的头文件
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-march=native" _march_native_works)    #　GNU编译器使用 -march=native 标志
check_cxx_compiler_flag("-xHost" _xhost_works)                  # Intel编译器使用 -xHost 标志

set(_CXX_FLAGS)                                     # 设置了一个空变量 _CXX_FLAGS
if (_march_native_works)
    message(STATUS "Using processor's vector instructions(-march=native compiler flag set)")
    set(_CXX_FLAGS "-march=native")
elseif (_xhost_works)
    message(STATUS "Using processor's vector instructions(-march=xHost compiler flag set)")
    set(_CXX_FLAGS "-xHost")
else ()
    message(STATUS "No suitable compiler flag found for vectorization")
endif ()

# 建立了两个target，使用加速和不使用
add_executable(linear-algebra-unoptimized linear-algebra.cpp)
target_link_libraries(linear-algebra-unoptimized PRIVATE Eigen3::Eigen)

add_executable(linear-algebra linear-algebra.cpp)
target_compile_options(linear-algebra PRIVATE ${_CXX_FLAGS})
target_link_libraries(linear-algebra PRIVATE Eigen3::Eigen)